/**
 * Cider
 * Copyright (c) 2004-2021 All Rights Reserved.
 */
package com.xunk.common.core.enums;

import org.apache.commons.collections4.map.CaseInsensitiveMap;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 杨文栋
 * @version DeviceEnum.java, v 0.1 2021-03-17 18:41 杨文栋 Exp $$
 */
public enum ClientEnum {
    /**
     * iphone
     */
    IPHONE("iPhone", 1),

    /**
     * android
     */
    ANDROID("Android", 2),

    /**
     * Web
     */
    WEB("Web", 3),

    /**
     * android_pad
     */
    ANDROID_PAD("Android_Pad", 4),

    /**
     * android_pad
     */
    IPAD("iPad", 5),

    /**
     * 未知设备
     */
    UNKNOWN("Unknown", 99);

    /**
     * 设备名称
     */
    String clientName;

    /**
     * 设备的type 代号(可以存数据库)
     */
    Integer clientType;

    ClientEnum(String deviceName, Integer deviceType) {
        this.clientName = deviceName;
        this.clientType = deviceType;
    }

    private static final Map<String, ClientEnum> DEVICE_NAME_ENUM_MAP = Arrays.stream(ClientEnum.values()).collect(Collectors.toMap(ClientEnum::getClientName, Function.identity(), (a, b) -> a, CaseInsensitiveMap::new));
    private static final Map<Integer, ClientEnum> DEVICE_TYPE_ENUM_MAP = Arrays.stream(ClientEnum.values()).collect(Collectors.toMap(ClientEnum::getClientType, Function.identity(), (a, b) -> a, CaseInsensitiveMap::new));

    public static ClientEnum parseClientEnum(String deviceName) {
        return DEVICE_NAME_ENUM_MAP.getOrDefault(deviceName, UNKNOWN);
    }

    public static ClientEnum parseClientEnum(Integer deviceType) {
        return DEVICE_TYPE_ENUM_MAP.getOrDefault(deviceType, UNKNOWN);
    }


    public String getClientName() {
        return clientName;
    }

    public Integer getClientType() {
        return clientType;
    }
}
